﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using TallyJ1Announcer.VoterRelated;

namespace TallyJ1Announcer.XmlSource
{
	[XmlRoot("Community")]
	public class XmlCommunity
	{
		[XmlElement(ElementName = "Person")]
		public List<XmlPerson> XmlPeople { get; set; }

		[XmlIgnore]
		public int TotalEligible
		{
			get
			{
				return XmlPeople
					.Where(x => x.AgeGroup == XmlPerson.XmlAgeGroup.Adult && !x.IneligibleToReceiveVotes)
					.Count();
			}
		}
		[XmlIgnore]
		public IList<Voter> Voted
		{
			get
			{
				return XmlPeople
					.Where(x => IncludeInDisplay(x.Voted))
					.OrderBy(x => x.LastName)
					.ThenBy(x=>x.FirstName)
					.Select(xmlPerson => xmlPerson.AsName()).ToList();
			}
			set { throw new NotImplementedException(); }
		}

		bool IncludeInDisplay(XmlPerson.XmlVoted voted)
		{
			switch (voted)
			{
				case XmlPerson.XmlVoted.No:
					return false;

				case XmlPerson.XmlVoted.VotedInPerson:
				case XmlPerson.XmlVoted.DroppedOff:
				case XmlPerson.XmlVoted.Mailed:
					return true;	
				
				default:
					throw new ArgumentOutOfRangeException("voted");
			}
		}

		[XmlAttribute(AttributeName = "Name")]
		public string Name { get; set; }
	}
}